package com.yq.interview.jvm.study;

import java.lang.ref.SoftReference;

/**
 * @ClassName SoftReferenceDemo
 * @Description 软引用
 * @Author yaoqiang
 * @Date 2020/11/25 11:12
 * @Version 1.0
 */
public class SoftReferenceDemo {
    public static void main(String[] args) {
       // softRefMemoryEnough();
        softRefMemoryNotEnough();
    }


    public static void softRefMemoryEnough(){
        Object obj1=new Object();
        SoftReference<Object> softReference=new SoftReference<>(obj1);
        System.out.println(obj1);
        System.out.println(softReference.get());
        obj1=null;
        System.gc();
        System.out.println(obj1);
        System.out.println(softReference.get());
    }

    /**
     * -Xms5m -Xmx5m -XX:+PrintGCDetails
     */
    public static void softRefMemoryNotEnough(){
        Object obj1=new Object();
        SoftReference<Object> softReference=new SoftReference<>(obj1);
        System.out.println(obj1);
        System.out.println(softReference.get());
        obj1=null;
        System.gc();
        try {
            byte[] bytes=new byte[30*1024*1024];
        }catch (Exception e){

        }finally {
            System.out.println(obj1);
            System.out.println(softReference.get());
        }

    }

}
